﻿using CERT.Services.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using RefactorThis.GraphDiff;


namespace CERT.Services
{
    public partial class NETServices : CERT.Services.INETServices
    {
        public Victim GetVictim(System.Linq.Expressions.Expression<Func<Victim, bool>> predicate)
        {
            Victim entry = null;

            using (var context = new NETEntities())
            {
                try
                {
                    entry = context.Set<Victim>().Include("People_Victim").FirstOrDefault<Victim>(predicate);
                }
                catch (Exception ex)
                { }

                return entry;
            }
        }

        public IEnumerable<Victim> GetVictims(System.Linq.Expressions.Expression<Func<Victim, bool>> predicate)
        {
            IEnumerable<Victim> entry = null;

            using (var context = new NETEntities())
            {
                try
                {
                    entry = context.Set<Victim>().Where<Victim>(predicate).ToList();
                }
                catch (Exception ex)
                { }

                return entry;
            }
        }
        public void DeleteVictim(int id)
        {

            //todo ideally, you check and see if this the user's only association.
            //if so, delete them.
            using (var context = new NETEntities())
            {
                var entity = context.Set<Victim>().FirstOrDefault(a => a.Id == id);
                context.Set<Victim>().Remove(entity);
                context.SaveChanges();
            }

        }

        public void UpdateVictim(Victim location, int locationId)
        {
           
            //http://refactorthis.wordpress.com/2012/12/11/introducing-graphdiff-for-entity-framework-code-first-allowing-automated-updates-of-a-graph-of-detached-entities/#comments

            using (var context = new NETEntities())
            {
                context.UpdateGraph<Victim>(location);


                var existing = context.Victims.Find(locationId);
                context.Entry(existing).CurrentValues.SetValues(location);


                context.SaveChanges();
            }

        }

        public void AddVictim(Victim entity)
        {
            using (var context = new NETEntities())
            {

               
                    var location = context.Locations.FirstOrDefault(a => a.Id == entity.LocationId);
                    entity.Location = location;
               location.Victims.Add(entity);
               // context.UpdateGraph<Location>(location, map => map
               //.OwnedCollection(p => p.Victims));
               context.Set<Victim>().Add(entity);
               context.SaveChanges();
              
                 }

             
           // }
        }

    }
}